from drf_haystack.viewsets import HaystackViewSet
from rest_framework import status
from rest_framework.generics import RetrieveAPIView, ListAPIView, UpdateAPIView, CreateAPIView
from rest_framework.response import Response

from apps.leadnews.models import Channel, Article, Comment
from apps.leadnews.serializers import ChannelSerializer, PutArticleSerializer, \
    ArticleCreateSerializer, CollectArticleSerializer, CommentDetailSerializer, \
    ChannelDetailSerializer, ArticleIndexSerializer
from apps.leadnews.utils import PageNum

from apps.user.utils import JwtAuthentication
from shicifang.utils.page import page


class ChannelAPIView(ListAPIView):
    queryset = Channel.objects.all()
    serializer_class = ChannelSerializer
    pagination_class = PageNum


class ArticleAPIView(CreateAPIView):
    authentication_classes = [JwtAuthentication]
    queryset = Article.objects.all()
    serializer_class = PutArticleSerializer

    def create(self, request, *args, **kwargs):
        data = request.data
        data["user"] = request.user.get("user_id")
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)
        data_rec = self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(data_rec, status=status.HTTP_201_CREATED, headers=headers)

    def perform_create(self, serializer):
        data_rec = serializer.save()
        data = {
            "articleid": data_rec.id,
            "message": 'ok',
            "success": True
        }
        return data


class GetArticleAPIView(RetrieveAPIView):
    """文章详情"""
    serializer_class = CommentDetailSerializer
    queryset = Article.objects.all()

class ArticleListView(ListAPIView):
    pagination_class = PageNum
    serializer_class = ChannelDetailSerializer

    def get_queryset(self):
        pk = self.kwargs['id']
        if pk == -1:
            return Article.objects.all()
        return Article.objects.filter(channel_id=pk)


# 评论文章
class ArticleCreateAPIView(CreateAPIView):
    authentication_classes = [JwtAuthentication]
    pagination_class = PageNum
    serializer_class = ArticleCreateSerializer
    queryset = Comment.objects.all()

    def create(self, request, *args, **kwargs):
        data = request.data
        data['user_id'] = request.user['user_id']
        serializer = ArticleCreateSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response({"message": "评论成功", "success": True}, status=status.HTTP_201_CREATED, headers=headers)

    def perform_create(self, serializer):
        serializer.save()



# 收藏
class CollectArticleView(UpdateAPIView):
    serializer_class = CollectArticleSerializer
    queryset = Article.objects.all()

    def update(self, request, *args, **kwargs):
        user = request.user
        id = self.kwargs.get('pk')
        article = Article.objects.get(id=id)
        users = article.collected_users.all()
        if user in users:
            article.collected_users.remove(user)
            return Response(data={'message': '取消收藏成功', 'success': True})
        else:
            article.collected_users.add(user)
            return Response(data={'message': '收藏成功', 'success': True})

# 搜索
class ArticleSearchViewSet(HaystackViewSet):
    """
    Article搜索
    """
    index_models = [Article]
    serializer_class = ArticleIndexSerializer
    pagination_class = page